package com.mindainfo.configCenter.apk.dao.impl;

import com.mindainfo.configCenter.apk.bean.ApkInfo;
import com.mindainfo.configCenter.apk.dao.IApkDao;
import com.mindainfo.configCenter.base.dao.impl.BaseDaoImpl;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.CriteriaContainer;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by MD-01 on 2016/11/17.
 */
@Repository
public class ApkDaoImpl extends BaseDaoImpl<ApkInfo> implements IApkDao{

    /**
     * 获取apk列表
     */
    public List<ApkInfo> list(String keyword, int start, int num){
        Query<ApkInfo> query = getDatastore().createQuery(ApkInfo.class);

        if(StringUtils.isNotEmpty(keyword)){
            query.or(
                    query.criteria("name").contains(keyword),
                    query.criteria("area").contains(keyword)
            );
        }
        return query.order("-createTime").offset(start).limit(num).asList();
    }

    public int listCount(String keyword){
        Query<ApkInfo> query = getDatastore().createQuery(ApkInfo.class);

        if(StringUtils.isNotEmpty(keyword)){
            query.or(
                    query.criteria("name").contains(keyword),
                    query.criteria("area").contains(keyword)
            );
        }
        return (int)query.order("-createTime").countAll();
    }
}
